import Vue from 'vue'

// initial state
const state = {
    // ============== AsidePage用到的变量 ==============
    wechats: [], // 当前用户绑定的微信
    currentWechat: {}, // 会话页面选中的微信
    // ============== ConversationFriends用到的变量 ====
    currentTab: 'LastChat', // ConversationFriends页面的tab
    lastChatPages: {}, // ConversationFriends页面的tab页面回显
    addressListPages: {}, // ConversationFriends页面的tab页面回显
    chatRoomPages: {}, // ConversationFriends页面的tab页面回显
    currentFriends: {}, // 会话页面每个微信选中的好友
    freshDisabled: false, // 刷新按钮不可用
    // ============== ConversationChats用到的变量 =======
    content: '', // 当前要发送的内容， 默认为空
    remark: '', // 要@的群成员的id
    contentType: 'Text', // 当前要发送的消息类型，默认为文本
    url: '', // 大图的url
    hbDetailVisible: false, // 显示红包详情
    hbDetail: {
        WeChatId: '', // 所属微信号
        Success: false,
        ErrMsg: '',
        HbUrl: '', //
        TotalNum: null, // 红包总个数
        TotalAmount: null, // 红包总金额
        RecNum: null, // 已收红包个数
        Records: [], // 已收红包个数
        RecAmount: null // 已收红包金额
    }, // 红包详情
    // ============== ConversationTools用到的变量 =======
    publicReply: [], // 公共话术
    publicTags: [], // 公共标签
    ghList: [], // 公众号列表
    ghMap: {}, // 公众号列表
    toduList:[],//任务列表
    daskDetail:{}, //任务详细
    dictlist:{},//字典
    notilist:[],//通知
    isinput:false,// 是否录入订单状态
    inputform:{},//工单录入表单
    chooseShow:false,//任務選擇
    itemshow:false,//任務详情展示
    menuVisible: false, // 右键菜单显示
    rebatesChannel: 1, // 任务处理-返款渠道
    rebatesChannelUrl: '', //任务处理-返款渠道-图片URL
    showAddWxPaymentcode: false,
    reloadTodo:false, //重新加載任務界面
    taskItemForm:{}, //任务修改
    taskDetailManager:[],//任务明细列表
    isModifyTaskDetail:false,//是否是修改任务详情
    
}

// getters
const getters = {
    // ============== AsidePage用到的变量 ==============
    wechats: state => state.wechats,
    wechatsLogin: state => {
        let loginedWechats = []
        for (const wechat of state.wechats) {
            if (wechat.IsOnline && wechat.IsLogined) {
                loginedWechats.push(wechat)
            }
        }
        return loginedWechats
    },
    currentWechat: state => state.currentWechat,
    currentWeChatId: state => {
        return state.currentWechat.WeChatId ? state.currentWechat.WeChatId : ''
    },
    currentFriendId: (state, getters) => {
        let currentWeChatId = getters.currentWeChatId
        let currentFriends = state.currentFriends
        let currentFriend = currentFriends[currentWeChatId]
        let currentFriendId = ''
        if (currentFriend) {
            currentFriendId = currentFriend.FriendId || currentFriend.UserName
        }
        return currentFriendId
    },
    // ============== ConversationFriends用到的变量 ====
    currentTab: state => state.currentTab,
    lastChatPages: state => state.lastChatPages,
    addressListPages: state => state.addressListPages,
    chatRoomPages: state => state.chatRoomPages,
    currentFriends: state => state.currentFriends,
    freshDisabled: state => state.freshDisabled,
    // ============== ConversationChats用到的变量 =======
    content: state => state.content,
    remark: state => state.remark,
    contentType: state => state.contentType,
    url: state => state.url,
    hbDetailVisible: state => state.hbDetailVisible,
    hbDetail: state => state.hbDetail,
    // ============== ConversationTools用到的变量 =======
    publicReply: state => state.publicReply,
    publicTags: state => state.publicTags,
    ghMap: state => state.ghMap,
    ghList: state => state.ghList,
    toduList: state =>state.toduList,
    daskDetail: state=>state.daskDetail,
    dictlist: state=>state.dictlist,
    notilist: state=>state.notilist,
    isinput: state=>state.isinput,
    inputform: state=>state.inputform,
    chooseShow:state=>state.chooseShow,
    itemshow:state=>state.itemshow,
    menuVisible:state=>state.menuVisible,
    rebatesChannel: state=>state.rebatesChannel,
    rebatesChannelUrl: state=>state.rebatesChannelUrl,
    showAddWxPaymentcode: state=>state.showAddWxPaymentcode,
    reloadTodo: state=>state.reloadTodo,
    taskItemForm: state=>state.taskItemForm,
    taskDetailManager: state=>state.taskDetailManager,
    isModifyTaskDetail: state=>state.isModifyTaskDetail,

    

    
    
}

// actions
const actions = {
    // 更新currentFriends
    UpdateCurrentFriends({ commit, getters }, friend) {
        // 更新当前的好友数据
        let currentFriends = getters.currentFriends
        // 当前的微信
        let currentWeChatId = getters.currentWeChatId
        // 当前的好友
        let currentFriend = currentFriends[currentWeChatId]
        if (friend.WeChatId === currentWeChatId && currentFriend && friend.FriendId === currentFriend.FriendId) {
            let fmsg = {
                wechatId: friend.WeChatId,
                friend: friend
            }
            commit('SET_CURRENT_FRIENDS', fmsg)
        }
    },
    // 更新currentFriends
    UpdateCurrentFriendsByRoom({ commit, getters }, room) {
        // 更新当前的好友数据
        let currentFriends = getters.currentFriends
        // 当前的微信
        let currentWeChatId = getters.currentWeChatId
        // 当前的好友
        let currentFriend = currentFriends[currentWeChatId]
        if (room.WeChatId === currentWeChatId && currentFriend && room.UserName === currentFriend.UserName) {
            let fmsg = {
                wechatId: room.WeChatId,
                friend: room
            }
            commit('SET_CURRENT_FRIENDS', fmsg)
        }
    },
}

// mutations
const mutations = {
    // ============== AsidePage用到的变量 ==============
    SET_WECHATS: (state, wechats) => {
        state.wechats = wechats
    },
    // 设置当前选中的微信
    SET_CURRENT_WECHAT: (state, wechat) => {
        state.currentWechat = wechat
    },
    // ============== ConversationFriends用到的变量 ====
    SET_FRESH_DISABLED: (state, flag) => {
        state.freshDisabled = flag
    },
    // 设置当前的nav
    SET_CURRENT_TAB: (state, tab) => {
        state.currentTab = tab
    },
    // 设置当前会话的当前页
    SET_LAST_CHAT_PAGES: (state, msg) => {
        Vue.set(state.lastChatPages, msg.wechatId, msg.page)
    },
    // 设置通讯录的当前页
    SET_ADDRESS_LIST_PAGES: (state, msg) => {
        Vue.set(state.addressListPages, msg.wechatId, msg.page)
    },
    // 设置群聊的当前页
    SET_CHAT_ROOM_PAGES: (state, msg) => {
        Vue.set(state.chatRoomPages, msg.wechatId, msg.page)
    },
    // 设置当前微信 所选的好友
    SET_CURRENT_FRIENDS: (state, msg) => {
        Vue.set(state.currentFriends, msg.wechatId, msg.friend)
    },
    // ============== ConversationChats用到的变量 =======
    // 设置消息内容
    SET_CONTENT: (state, content) => {
        state.content = content
    },
    SET_REMARK: (state, remark) => {
        state.remark = remark
    },
    // 设置消息类型
    SET_CONTENT_TYPE: (state, type) => {
        state.contentType = type
    },
    // 设置大图/视频 的url
    SET_URL: (state, url) => {
        state.url = url
    },
    // 设置显示红包详情
    SET_HB_DETAIL_VISIBLE: (state, visible) => {
        state.hbDetailVisible = visible
    },
    // 设置显示红包详情
    SET_HB_DETAIL: (state, hbdetail) => {
        for (const key in hbdetail) {
            if (hbdetail.hasOwnProperty(key)) {
                Vue.set(state.hbDetail, key, hbdetail[key])
            }
        }
    },
    // ============== ConversationTools用到的变量 ====
    // 设置公共话术
    SET_PUBLIC_REPLY: (state, publicReply) => {
        state.publicReply = publicReply
    },
    // 设置任务列表
    GET_TODU_LIST: (state, publicReply) => {
        state.toduList = publicReply
    },
    // 设置任务明细列表数据
    SET_TASKDETAILMANAGER: (state, publicReply) => {
        state.taskDetailManager = publicReply
    },
    // 获取任务详情
    SET_DASK_DETAIL:(state, publicReply) => {
        state.daskDetail = publicReply
    },
    // 设置字典列表
    SET_DICT_LIST:(state, publicReply) => {
        state.dictlist = publicReply
    },
    // 消息列表
    SET_NOTI_LIST:(state, publicReply) => {
        state.notilist = publicReply
    },
    // 设置公共标签
    SET_PUBLIC_TAGS: (state, publicTags) => {
        state.publicTags = publicTags
    },
    // 设置公共号列表
    SET_GH_LIST: (state, ghList) => {
        state.ghList = ghList.Contacts
    },
    SET_GH_MAP: (state, ghMap) => {
        Vue.set(state.ghMap, ghMap.WeChatId, ghMap.Contacts)
    },
    SET_ISINPUT:(state, publicTags)=>{
        state.isinput=publicTags;
    },
    SET_INPUTFORM:(state, publicTags)=>{
        console.log(publicTags)
        state.inputform=publicTags;
    },
    // 设置选择任务弹窗状态
    SET_CHHOOSESHOW:(state, chooseShow)=>{
        state.chooseShow=chooseShow;
    },
    SET_ITEMSHOW:(state, itemshow)=>{
        state.itemshow=itemshow;
    },

    SET_MENUSHOW:(state, menuVisible)=>{
        state.menuVisible=menuVisible;
    },
    // 设置返款渠道
    SET_REBATESCHANNEL:(state, rebatesChannel)=>{
        state.rebatesChannel=rebatesChannel;
    },
    // 设置返款渠道url
    SET_REBATESCHANNELURL:(state, rebatesChannelUrl)=>{
        state.rebatesChannelUrl=rebatesChannelUrl;
    },
    // 设置返款渠道url
    SET_SHOWADDWXPAYMENTCODE:(state, showAddWxPaymentcode)=>{
        state.showAddWxPaymentcode=showAddWxPaymentcode;
    },

    // 设置返款渠道url
    SET_RELOADTODO:(state, reloadTodo)=>{
        state.reloadTodo=reloadTodo;
    },
    // 设置任务详情表单
    SET_TASKITEMFORM:(state,taskItemForm)=>{
        state.taskItemForm=taskItemForm;
    },
    SET_ISMODIFYTASKDETAIL:(state,isModifyTaskDetail)=>{
        state.isModifyTaskDetail=isModifyTaskDetail;
    },
    

    
    

    

    // GET_INPUTFORM:(state)=>{
    //     return state.inputform;
    // },
    // 修改微信的登陆状态和在线状态
    MODIFY_WECHATS: (state, newState) => {
        for (const wechat of state.wechats) {
            if (wechat.WeChatId === newState.WeChatId) {
                wechat.IsLogined = newState.IsLogined
                wechat.IsOnline = newState.IsOnline
                break
            }
        }
    },
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}
